<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>10. 向表单添加条件控制 | 被删的前端游乐场</title>
    <meta name="description" content="Just playing around">
    
    
    <link rel="preload" href="/front-end-playground/assets/css/0.styles.a683ed26.css" as="style"><link rel="preload" href="/front-end-playground/assets/js/app.e5cf4092.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/3.c853767c.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/2.eed49fdd.js" as="script"><link rel="preload" href="/front-end-playground/assets/js/50.e561d7c0.js" as="script">
    <link rel="stylesheet" href="/front-end-playground/assets/css/0.styles.a683ed26.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/front-end-playground/" class="home-link router-link-active"><!----> <span class="site-name">被删的前端游乐场</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端基础</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/front-end-playground/" class="nav-link">概述</a></div><div class="nav-item"><a href="/front-end-playground/front-end-basic/" class="nav-link">前端基础</a></div><div class="nav-item"><a href="/front-end-playground/vue/" class="nav-link">Vue学习</a></div><div class="nav-item"><a href="/front-end-playground/wxapp/" class="nav-link">小程序学习</a></div><div class="nav-item"><a href="/front-end-playground/front-end-others/" class="nav-link router-link-active">百家齐放</a></div><div class="nav-item"><a href="/front-end-playground/front-end-addon/" class="nav-link">前端的进击</a></div><div class="nav-item"><a href="/front-end-playground/faq.html" class="nav-link">FAQ</a></div> <a href="https://github.com/godbasin/front-end-playground" target="_blank" rel="noopener noreferrer" class="repo-link">
    Github
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0" style="padding-top:10px;"><div class="kitty-main" data-v-0cd75e06><span class="stand" data-v-0cd75e06></span> <div class="cat" data-v-0cd75e06><div class="body" data-v-0cd75e06></div> <div class="head" data-v-0cd75e06><div class="ear" data-v-0cd75e06></div> <div class="ear" data-v-0cd75e06></div></div> <div class="face" data-v-0cd75e06><div class="nose" data-v-0cd75e06></div> <div class="whisker-container" data-v-0cd75e06><div class="whisker" data-v-0cd75e06></div> <div class="whisker" data-v-0cd75e06></div></div> <div class="whisker-container" data-v-0cd75e06><div class="whisker" data-v-0cd75e06></div> <div class="whisker" data-v-0cd75e06></div></div></div> <div class="tail-container" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06><div class="tail" data-v-0cd75e06></div></div></div></div></div></div></div></div></div></div> <p class="sidebar-heading open"><span>玩转 Angular</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/front-end-playground/front-end-others/angular-free/0-about-angular-from-1-to-4.html" class="sidebar-link">谈谈 AngularJS 到 Angular</a></li><li><a href="/front-end-playground/front-end-others/angular-free/0-di-in-angular.html" class="sidebar-link">谈谈 Angular 的依赖注入</a></li><li><a href="/front-end-playground/front-end-others/angular-free/1-init-angular-app-with-webpack.html" class="sidebar-link">1. 用 Webpack 启动 Angular 应用</a></li><li><a href="/front-end-playground/front-end-others/angular-free/2-inprove-app-configuration.html" class="sidebar-link">2. 改善应用配置</a></li><li><a href="/front-end-playground/front-end-others/angular-free/3-init-routes-and-assets.html" class="sidebar-link">3. 启用路由和添加静态资源</a></li><li><a href="/front-end-playground/front-end-others/angular-free/4-create-sidebar.html" class="sidebar-link">4. 制作左侧自动定位菜单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/5-custom-input-component.html" class="sidebar-link">5. 自定义 input 表单控件</a></li><li><a href="/front-end-playground/front-end-others/angular-free/6-two-form-building.html" class="sidebar-link">6. 模型驱动和模板驱动的表单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/7-dynamic-form.html" class="sidebar-link">7. 创建动态表单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/8-radio-and-checkbox.html" class="sidebar-link">8. 表单的 radio 和 checkbox</a></li><li><a href="/front-end-playground/front-end-others/angular-free/9-upload-iamge.html" class="sidebar-link">9. 图片上传控件</a></li><li><a href="/front-end-playground/front-end-others/angular-free/10-add-conditions.html" class="active sidebar-link">10. 向表单添加条件控制</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/angular-free/10-add-conditions.html#表单条件" class="sidebar-link">表单条件</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/angular-free/10-add-conditions.html#带输入的选择" class="sidebar-link">带输入的选择</a></li><li class="sidebar-sub-header"><a href="/front-end-playground/front-end-others/angular-free/10-add-conditions.html#结束语" class="sidebar-link">结束语</a></li></ul></li><li><a href="/front-end-playground/front-end-others/angular-free/11-option-dialog.html" class="sidebar-link">11. 使用动态表单制作选项配置对话框</a></li><li><a href="/front-end-playground/front-end-others/angular-free/12-config-and-create-form.html" class="sidebar-link">12. 配置以及生成表单</a></li><li><a href="/front-end-playground/front-end-others/angular-free/13-dynamic-list.html" class="sidebar-link">13. 动态列表配置</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Cycle.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>D3.js 和 Tree</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>试试 Three.js</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0" style="padding-top:;"><!----> <p class="sidebar-heading"><span>BOX2D.js 文档与教程</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><p>该系列用于探索一些 Angular 中灵活或者新的用法。本文介绍向表单添加简单的条件控制的过程。</p> <h2 id="表单条件"><a href="#表单条件" aria-hidden="true" class="header-anchor">#</a> 表单条件</h2> <h3 id="常用表单条件"><a href="#常用表单条件" aria-hidden="true" class="header-anchor">#</a> 常用表单条件</h3> <p>很多时候，我们的表单需要添加一些条件，例如：</p> <ul><li>选择是否订阅邮件，是则需要填写邮件</li> <li>针对不同的选择展示不同的表单控件</li></ul> <p>这里我们只考虑比较容易实现的：</p> <ul><li>校验
<ul><li>通过某个控件值的比较，来作为判断标准</li> <li>传入其他控件的<code>key</code>，通过常用比较<code>&gt;</code>/<code>&gt;=</code>/<code>==</code>等，来控制显示</li></ul></li> <li>条件（只能二选一），作为以上校验的叠加方式选择
<ul><li>'&amp;&amp;': 与，需同时满足多个条件</li> <li>'||': 或，满足其中一个条件即可</li></ul></li></ul> <p>通过以上我们可以定义基本配置接口结构：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 校验配置</span>
<span class="token keyword">export</span> <span class="token keyword">interface</span> <span class="token class-name">IHiddenValidate</span> <span class="token punctuation">{</span>
  key<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  validate<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  param<span class="token punctuation">:</span> string <span class="token operator">|</span> number<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 条件配置</span>
<span class="token keyword">export</span> <span class="token keyword">interface</span> <span class="token class-name">IHiddenCondition</span> <span class="token punctuation">{</span>
  condition<span class="token punctuation">:</span> <span class="token string">&quot;||&quot;</span> <span class="token operator">|</span> <span class="token string">&quot;&amp;&amp;&quot;</span><span class="token punctuation">;</span>
  validations<span class="token punctuation">:</span> IHiddenValidate<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 自定义单个控件配置</span>
<span class="token keyword">export</span> <span class="token keyword">interface</span> <span class="token class-name">ICustomControl</span> <span class="token punctuation">{</span>
  type<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  label<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  key<span class="token punctuation">:</span> string<span class="token punctuation">;</span>
  validations<span class="token operator">?</span><span class="token punctuation">:</span> IValidations<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  options<span class="token operator">?</span><span class="token punctuation">:</span> IOptions<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
  limit<span class="token operator">?</span><span class="token punctuation">:</span> ILimit<span class="token punctuation">;</span>
  hiddenWhen<span class="token operator">?</span><span class="token punctuation">:</span> IHiddenCondition<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="条件判断"><a href="#条件判断" aria-hidden="true" class="header-anchor">#</a> 条件判断</h3> <p>我们针对上面的数据结构，需要进行条件判断，返回布尔值来控制表单的显示或隐藏。</p> <p>首先，我们添加一个用于计算条件的函数：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">function</span> <span class="token function">validate</span><span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> condition</span><span class="token punctuation">)</span><span class="token punctuation">:</span> boolean <span class="token punctuation">{</span>
  <span class="token keyword">switch</span> <span class="token punctuation">(</span>condition<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">case</span> <span class="token string">&quot;&gt;&quot;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> a <span class="token operator">&gt;</span> b<span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">&quot;&gt;=&quot;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> a <span class="token operator">&gt;=</span> b<span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">&quot;==&quot;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> a <span class="token operator">==</span> b<span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">&quot;===&quot;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> a <span class="token operator">===</span> b<span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">&quot;&gt;&quot;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> a <span class="token operator">&gt;</span> b<span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">&quot;&gt;=&quot;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> a <span class="token operator">&gt;=</span> b<span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">&quot;&amp;&amp;&quot;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> a <span class="token operator">&amp;&amp;</span> b<span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">&quot;||&quot;</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> a <span class="token operator">||</span> b<span class="token punctuation">;</span>
    <span class="token keyword">case</span> <span class="token string">&quot;indexOf&quot;</span><span class="token punctuation">:</span> <span class="token comment">// 用于数组判断</span>
      <span class="token keyword">return</span> a<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">default</span><span class="token punctuation">:</span>
      <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>然后我们开始处理较复杂的条件和校验，先将多个校验计算出来，再使用条件合并：</p> <div class="language-js extra-class"><pre class="language-js"><code>    <span class="token function">isHidden</span><span class="token punctuation">(</span><span class="token parameter">control<span class="token punctuation">:</span> ICustomControl</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">let</span> hidden <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span> <span class="token comment">// 初始化显示</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>control<span class="token punctuation">.</span>hiddenWhen <span class="token operator">&amp;&amp;</span> control<span class="token punctuation">.</span>hiddenWhen<span class="token punctuation">.</span>validations <span class="token operator">&amp;&amp;</span> control<span class="token punctuation">.</span>hiddenWhen<span class="token punctuation">.</span>validations<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            control<span class="token punctuation">.</span>hiddenWhen<span class="token punctuation">.</span>validations<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">valid</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
                <span class="token comment">// 条件计算</span>
                hidden <span class="token operator">=</span> <span class="token function">validate</span><span class="token punctuation">(</span>
                    hidden<span class="token punctuation">,</span>
                    <span class="token function">validate</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>dynamicForm<span class="token punctuation">.</span>value<span class="token punctuation">[</span>valid<span class="token punctuation">.</span>key<span class="token punctuation">]</span><span class="token punctuation">,</span> valid<span class="token punctuation">.</span>param<span class="token punctuation">,</span> valid<span class="token punctuation">.</span>validate<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 校验计算</span>
                    control<span class="token punctuation">.</span>hiddenWhen<span class="token punctuation">.</span>condition
                <span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> hidden<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre></div><p>通过这个方法我们就能控制是否显示对应表单控件了：</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">*ngFor</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>let control of config<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>form-group<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">*ngIf</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>!isHidden(control)<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token comment">&lt;!--具体控件--&gt;</span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><h3 id="实例配置"><a href="#实例配置" aria-hidden="true" class="header-anchor">#</a> 实例配置</h3> <p>我们可以通过一个简单的邮件接收配置来检验：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">export</span> <span class="token keyword">const</span> customForms<span class="token punctuation">:</span> ICustomControl<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;radio&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;是否接收邮件&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;emailReceived&quot;</span><span class="token punctuation">,</span>
    options<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;1&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;是&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> id<span class="token punctuation">:</span> <span class="token string">&quot;0&quot;</span><span class="token punctuation">,</span> text<span class="token punctuation">:</span> <span class="token string">&quot;否&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  <span class="token punctuation">{</span>
    type<span class="token punctuation">:</span> <span class="token string">&quot;text&quot;</span><span class="token punctuation">,</span>
    label<span class="token punctuation">:</span> <span class="token string">&quot;Email&quot;</span><span class="token punctuation">,</span>
    key<span class="token punctuation">:</span> <span class="token string">&quot;email&quot;</span><span class="token punctuation">,</span>
    validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
      <span class="token punctuation">{</span>
        type<span class="token punctuation">:</span> <span class="token string">&quot;email&quot;</span><span class="token punctuation">,</span>
        message<span class="token punctuation">:</span> <span class="token string">&quot;Email格式不正确&quot;</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">]</span><span class="token punctuation">,</span>
    hiddenWhen<span class="token punctuation">:</span> <span class="token punctuation">{</span>
      condition<span class="token punctuation">:</span> <span class="token string">&quot;||&quot;</span><span class="token punctuation">,</span>
      validations<span class="token punctuation">:</span> <span class="token punctuation">[</span>
        <span class="token punctuation">{</span>
          key<span class="token punctuation">:</span> <span class="token string">&quot;emailReceived&quot;</span><span class="token punctuation">,</span>
          validate<span class="token punctuation">:</span> <span class="token string">&quot;==&quot;</span><span class="token punctuation">,</span>
          param<span class="token punctuation">:</span> <span class="token string">&quot;0&quot;</span>
        <span class="token punctuation">}</span>
      <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre></div><p>效果如下：</p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496326365%281%29.png" alt="iamge"> <img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496326383%281%29.png" alt="iamge"></p> <h2 id="带输入的选择"><a href="#带输入的选择" aria-hidden="true" class="header-anchor">#</a> 带输入的选择</h2> <p>很多时候我们需要有这样的表单控件：</p> <ul><li>checkbox 勾选后可自定义填写</li> <li>radio 选择自定义填写</li></ul> <p>这里我们简单做个组件封装吧。</p> <h3 id="带输入的-checkbox"><a href="#带输入的-checkbox" aria-hidden="true" class="header-anchor">#</a> 带输入的 checkbox</h3> <p>我们先来定义我们想要的效果：</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">*ngFor</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>let op of options<span class="token punctuation">&quot;</span></span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>form-check<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span>
    <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>checkbox<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">[name]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>value<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">[checked]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>model[op.id] &amp;&amp; model[op.id].checked<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">(click)</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>setValue(op)<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">[disabled]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>disabled<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">[value]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>op.id<span class="token punctuation">&quot;</span></span>
  <span class="token punctuation">/&gt;</span></span>{{op.text}}
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>input</span>
    <span class="token attr-name">*ngIf</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>op.withInput<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>form-control form-inline-input<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">[type]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>type<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">[disabled]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>!(model[op.id] &amp;&amp; model[op.id].checked)<span class="token punctuation">&quot;</span></span>
    <span class="token attr-name">[(ngModel)]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>model[op.id].value<span class="token punctuation">&quot;</span></span>
  <span class="token punctuation">/&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><p>由于多了自定义填写，通过绑定选中数组的形式也不适用了，我们需要通过对象的方式来绑定返回值：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 这里为了方便表达清楚结构，使用了不合规范的表示，莫介意</span>
model <span class="token operator">=</span> <span class="token punctuation">{</span>
  id<span class="token punctuation">:</span> <span class="token punctuation">{</span>
    checked<span class="token punctuation">:</span> boolen<span class="token punctuation">,</span>
    value<span class="token punctuation">:</span> string <span class="token operator">|</span> number
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><p>用的实现还是前面自定义表单的那一套：</p> <div class="language-js extra-class"><pre class="language-js"><code>@<span class="token function">Component</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    selector<span class="token punctuation">:</span> <span class="token string">'checkbox-with-input'</span><span class="token punctuation">,</span>
    template<span class="token punctuation">:</span> <span class="token template-string"><span class="token string">`...同上`</span></span><span class="token punctuation">,</span>
    providers<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token function">customInputAccessor</span><span class="token punctuation">(</span>CheckboxWithTextComponent<span class="token punctuation">)</span><span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">export</span> <span class="token keyword">class</span> <span class="token class-name">CheckboxWithTextComponent</span> <span class="token keyword">implements</span> <span class="token class-name">OnInit</span> <span class="token punctuation">{</span>
    @<span class="token function">Input</span><span class="token punctuation">(</span><span class="token punctuation">)</span> options<span class="token punctuation">:</span> IOptions<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span> <span class="token comment">// object: {id, text} or array: []</span>
    @<span class="token function">Input</span><span class="token punctuation">(</span><span class="token punctuation">)</span> disabled<span class="token punctuation">:</span> boolean <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    @<span class="token function">Input</span><span class="token punctuation">(</span><span class="token punctuation">)</span> type<span class="token punctuation">:</span> string <span class="token operator">=</span> <span class="token string">'text'</span><span class="token punctuation">;</span>

    <span class="token keyword">private</span> model<span class="token punctuation">:</span> any <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">// 控件的值</span>
    <span class="token keyword">private</span> <span class="token function-variable function">onChange</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token parameter">_<span class="token punctuation">:</span> any</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token function-variable function">onTouched</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">void</span><span class="token punctuation">;</span>

    <span class="token function">ngOnInit</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 初始化model的值</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>options<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token parameter">op</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
            <span class="token keyword">this</span><span class="token punctuation">.</span>model<span class="token punctuation">[</span>op<span class="token punctuation">.</span>id<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span>
                checked<span class="token punctuation">:</span> <span class="token boolean">false</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>op<span class="token punctuation">.</span>withInput<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>model<span class="token punctuation">[</span>op<span class="token punctuation">.</span>id<span class="token punctuation">]</span><span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">setValue</span><span class="token punctuation">(</span><span class="token parameter">op<span class="token punctuation">:</span> any</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 选择某选项的时候处理</span>
        <span class="token keyword">const</span> isChecked <span class="token operator">=</span> <span class="token operator">!</span><span class="token keyword">this</span><span class="token punctuation">.</span>model<span class="token punctuation">[</span>op<span class="token punctuation">.</span>id<span class="token punctuation">]</span><span class="token punctuation">.</span>checked<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>model<span class="token punctuation">[</span>op<span class="token punctuation">.</span>id<span class="token punctuation">]</span><span class="token punctuation">.</span>checked <span class="token operator">=</span> isChecked<span class="token punctuation">;</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">onChange</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>model<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 下面的方法参照前面几节</span>
    <span class="token comment">// onBlur</span>
    <span class="token comment">// writeValue</span>
    <span class="token comment">// registerOnChange</span>
    <span class="token comment">// registerOnTouched</span>
<span class="token punctuation">}</span>
</code></pre></div><p>然后我们在动态表单里面添加进去：</p> <div class="language-html extra-class"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>checkbox-with-input</span>
  <span class="token attr-name">*ngIf</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>control.type === 'checkbox-with-text'<span class="token punctuation">&quot;</span></span>
  <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>text<span class="token punctuation">&quot;</span></span>
  <span class="token attr-name">[options]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>control.options<span class="token punctuation">&quot;</span></span>
  <span class="token attr-name">[formControlName]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>control.key<span class="token punctuation">&quot;</span></span>
<span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>checkbox-with-input</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>checkbox-with-input</span>
  <span class="token attr-name">*ngIf</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>control.type === 'checkbox-with-number'<span class="token punctuation">&quot;</span></span>
  <span class="token attr-name">type</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>number<span class="token punctuation">&quot;</span></span>
  <span class="token attr-name">[options]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>control.options<span class="token punctuation">&quot;</span></span>
  <span class="token attr-name">[formControlName]</span><span class="token attr-value"><span class="token punctuation">=</span><span class="token punctuation">&quot;</span>control.key<span class="token punctuation">&quot;</span></span>
<span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>checkbox-with-input</span><span class="token punctuation">&gt;</span></span>
</code></pre></div><p>而带输入的<code>radio</code>大家就自己解决吧，本骚年的项目代码里面有，基本与 checkbox 的很相像。</p> <p>结合上面，最终我们的效果图如下：</p> <p><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/1496325904%281%29.png" alt="image"></p> <h2 id="结束语"><a href="#结束语" aria-hidden="true" class="header-anchor">#</a> 结束语</h2> <p>这里我们只针对简单的条件进行表单校验，并且这里只能匹配一些简单绑定的值，像多选等因为绑定的值为对象，暂无法进行条件判断。<br>
一些复杂的控制功能或许需要我们通过其他方式进行吧，毕竟很多时候更广的通用性也会增加设计的复杂度、消耗使用的简便性。<br> <a href="https://github.com/godbasin/godbasin.github.io/tree/blog-codes/angular2-free/10-add-conditions" target="_blank" rel="noopener noreferrer">此处查看项目代码<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a><br> <a href="http://angular2-free.godbasin.com/10-add-conditions/index.html#/home/page-setting" target="_blank" rel="noopener noreferrer">此处查看页面效果<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></div> <!----> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/godbasin/front-end-playground/edit/sourcecode/docs/front-end-others/angular-free/10-add-conditions.md" target="_blank" rel="noopener noreferrer">帮阿猪改善此页面！</a> <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></div> <!----> <blockquote>部分文章中使用了一些网站的截图，如果涉及侵权，请告诉我删一下谢谢~</blockquote> <div style="margin-top:30px;"><div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-sm-0 el-col-md-2 el-col-lg-4" style="padding-left:10px;padding-right:10px;display:block;"><div style="width:1px;height:1px;"></div></div> <div class="el-col el-col-24 el-col-sm-24 el-col-md-18 el-col-lg-16" style="padding-left:10px;padding-right:10px;"><div class="el-card box-card is-always-shadow"><div class="el-card__header"><div class="clearfix"><span>温馨提示喵</span></div></div><div class="el-card__body"> <div class="el-row" style="margin-left:-10px;margin-right:-10px;"><div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="el-image"><div class="image-slot"><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/loading.gif" style="width:100%;"></div><!----></div></div> <div class="el-col el-col-24 el-col-xs-24 el-col-sm-12" style="padding-left:10px;padding-right:10px;"><div class="copyright-text"><div>本文版权归作者所有，欢迎转载，但未经作者同意必须保留此段声明，且在文章页面明显位置给出原文连接，否则保留追究法律责任的权利。</div> <div>出处：被删的前端游乐场</div> <div>作者：<a href="https://github.com/godbasin" target="_blank">被删</a></div></div></div></div></div></div></div></div></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
        ←
        <a href="/front-end-playground/front-end-others/angular-free/9-upload-iamge.html" class="prev">
          9. 图片上传控件
        </a></span> <span class="next"><a href="/front-end-playground/front-end-others/angular-free/11-option-dialog.html">
          11. 使用动态表单制作选项配置对话框
        </a>
        →
      </span></p></div>  <div class="gitalk-container theme-default-content"><div id="gitalk-container" class="content"></div></div></main> <div id="kitty-container"><span><div role="tooltip" id="el-popover-8500" aria-hidden="true" class="el-popover el-popper" style="width:undefinedpx;display:none;"><!----><img src="https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/2code2.jpg" class="image"> <div class="text">牡羊猪的猫粮罐</div> </div><div id="kitty" style="background:url(https://github-imglib-1255459943.cos.ap-chengdu.myqcloud.com/assets/img/kitty2.svg);"></div></span> <div class="el-dialog__wrapper" style="display:none;"><div role="dialog" aria-modal="true" aria-label="牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）" class="el-dialog" style="margin-top:15vh;"><div class="el-dialog__header"><span class="el-dialog__title">牡羊猪是这样渐渐胖成猪的喵（点击图片可以切换噢）</span><button type="button" aria-label="Close" class="el-dialog__headerbtn"><i class="el-dialog__close el-icon el-icon-close"></i></button></div><!----><!----></div></div></div></div><div class="global-ui"></div></div>
    <script src="/front-end-playground/assets/js/app.e5cf4092.js" defer></script><script src="/front-end-playground/assets/js/3.c853767c.js" defer></script><script src="/front-end-playground/assets/js/2.eed49fdd.js" defer></script><script src="/front-end-playground/assets/js/50.e561d7c0.js" defer></script>
  </body>
</html>
